import { Controller, Post, Body, Req, Put, UseGuards } from '@nestjs/common';
import { UserService } from './user.service';
import { RegisterDto, SignInDto, VerifyDto } from './dto/user.dto';
import { AuthGuard } from '../../@nt/guards/auth.guard';
import { RestController } from '../../@nt';

// 用户控制器
@Controller('user')
export class UserController extends RestController {
  constructor(protected readonly service: UserService) {
    super(service);
  }

  // 注册
  @Post('signup')
  async create(@Body() body: RegisterDto): Promise<any> {
    if (!this.isEmail(body.email)) {
      return this.returnJson(1001, null, '邮箱格式错误');
    }
    return await this.service.create(body);
  }

  // 登录
  @Post('signin')
  async singIn(@Body() body: SignInDto): Promise<any> {
    return await this.service.signIn(body);
  }

  // 验证邮箱
  @Put('verifyEmail')
  @UseGuards(AuthGuard)
  async verifyEmail(
    @Body() body: VerifyDto,
    @Req() req: Request,
  ): Promise<any> {
    const user = req['user'];
    return await this.service.verifyEmail(body, user);
  }
}
